<?php

namespace app\api\controller;

use app\admin\model\Advertisement;
use app\admin\model\Article;
use app\admin\model\ArticleCategory;
use app\admin\model\VideoCategory;
use think\Controller;
use think\Db;
use think\Request;
use think\Validate;
use app\api\model\Api;
use think\validate\ValidateRule;

class AppController extends BaseController
{
    protected $rule; // 验证参数
    protected $message;
    protected $param;
    public function initialize(){
       parent::initialize();
       $this->param = $this->request->param();
    }
    public function getinfo(){
      $user =   Db::name('member')
          ->field('level_name')
          ->field('nickname')
          ->field('brokerage_price')
          ->field('discount')
          ->field('phone')
          ->field('member_id as tag')
          ->field('currency')
          ->leftJoin('member_level','w_member.level_id=member_level.level_id')
          ->find($this->userInfo->user_id);
        $this->encode_json(200,'获取用户信息',$user);
    }

    public function _empty(){

    }
    public function input(){

        echo json_encode(['code'=>21,'msg'=>'测试','data'=>input('post.')]);
    }
    public function ad(){
        $ad = new Advertisement();
        $ads =$ad->select();
        foreach($ads as &$val){
            $val['advertising_img'] = 'http://'.$_SERVER['HTTP_HOST'].  $val['advertising_img'];
        }
        $this->encode_json(200,'获取成功',$ads);
    }
    public function article(){
            $cat = new ArticleCategory();
             $article = $cat->with('article')->select();
        $this->encode_json(200,'获取成功',$article);
    }
    //佣金提现
    public function cash(Request $request){
        $user_id = $this->userInfo->user_id;
        $param = $request->param();
        $member = Db::name('member')->find($user_id);
        if ($member['brokerage_price']>= $param['money']){
            $result =Db::name('cash')->insert(['cash_money'=>$param['money'],'cash_state'=>'0','account'=>$param['account'],'member_id'=>$user_id,'name'=>$param['name']]);
            if ($result){
                $result =Db::name('member')->where('member_id',$user_id)->setDec('brokerage_price', $param['money']);
                $this->encode_json(200,'申请成功！');
            }
        }else{
            $this->encode_json(4000,'余额不足');
        }
    }
    public function video(){
        $cat = new VideoCategory();
        $article = $cat->with(['video'=>function($query){
            $url =  'http://'.$_SERVER['HTTP_HOST'];
            $query->field('video_path,concat("'.$url.'",video_path) as video_path,category_id,video_title,video_desc');
        }])->select();
        $this->encode_json(200,'获取成功',$article);
    }
    public function lower_level(){
       $member =  Db::name('member')
            ->page(input('get.page',input('get.limit')))
           ->where('subordinate_member_id',$this->userInfo->user_id)->select();
       $count=Db::name('member')->where('subordinate_member_id',$this->userInfo->user_id)->count();
       $this->encode_json(200,'获取成功', ['list'=>$member,'count'=>$count]);
    }

    public function cash_log(){
        $cash_log  = Db::name('cash_back_log')
            ->field('phone')
            ->field('recharge_order_id')
            ->field('source_member_id')
            ->field('cash_back')
            ->field('w_recharge_order.add_time')
            ->field('pay_type')
            ->field('amount')
           ->page(input('get.page',input('get.limit')))
            ->leftJoin('w_recharge_order','w_recharge_order.id=w_cash_back_log.recharge_order_id')
            ->leftJoin('w_member','w_member.member_id=w_cash_back_log.source_member_id');
        $list = $cash_log->select();
        $count = $cash_log->count();
        $this->encode_json(200,'获取成功', ['list'=>$list,'count'=>$count]);
    }
    public function recharge_order(){
        $recharge_order = Db::name('recharge_order')
            ->field('amount')
            ->field('order_sn')
            ->field('add_time')
            ->field('describe')
            ->field('pay_type')
            ->field('pay_state')
            ->page(input('get.page',input('get.limit')))
            ->where('member_id',$this->userInfo->user_id);
        $list = $recharge_order->select();
        $count = $recharge_order->count();
        $this->encode_json(200,'获取成功', ['list'=>$list,'count'=>$count]);
    }
    public function get_price_list(){
        $api_price = Db::name('api')
            ->alias('a')
            ->leftJoin('api_price p','a.api_id=p.api_id')
            ->select();
        $level = Db::name('member_level')->field('level_name,level_id')->select();

    }
}
